//
//  UIImage+Scale.swift
//  imagePicker
//
//  Created by lzing on 16/4/5.
//  Copyright © 2016年 LZING. All rights reserved.
//

import UIKit

extension UIImage {
    ///  根据宽度宽度等比例缩放
    func scaleImage(newWidth : CGFloat = 300) -> UIImage {
        if self.size.width < newWidth {
            return self
        }
        // 等比例缩放
        let newHeight = newWidth * size.height / size.width
        let newSize = CGSize(width: newWidth, height: newHeight)
        // 绘图
        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
        // 获取上下文图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        return newImage
    }
}
